package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoMjYkm;
import com.ocom.accesservice.service.MjYkmService;
import com.ocom.common.entity.access.EntityMjYkm;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MjYkmServiceImpl extends ServiceImpl<IDaoMjYkm, EntityMjYkm> implements MjYkmService {

    @Autowired
    private IDaoMjYkm daoMjYkm;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjYkm YkmInfo(Long comId, Long areaId) {
        QueryWrapper<EntityMjYkm> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("area_id",areaId);
        return daoMjYkm.selectOne(query);
    }
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void initYkmInfo(Long comId, Long areaId) {
        EntityMjYkm entityMjYkm = new EntityMjYkm();
        entityMjYkm.setComId(comId);
        entityMjYkm.setAreaId(areaId);
        entityMjYkm.setTitle("健康码自动核验系统");
        entityMjYkm.setIsEnable(true);
        entityMjYkm.setColor(true);
        entityMjYkm.setColorRed(false);
        entityMjYkm.setColorYellow(false);
        entityMjYkm.setColorGreen(true);
        entityMjYkm.setCheckResource(true);
        entityMjYkm.setCheckResourceYin(true);
        entityMjYkm.setCheckResourceYang(false);
        daoMjYkm.insert(entityMjYkm);
    }

    //修改粤康码规则信息（Ykm/modify）
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void YkmModify(Long comId,EntityMjYkm entityMjYkm) {
        QueryWrapper<EntityMjYkm> query = Wrappers.query();
        query.eq("com_id",entityMjYkm.getComId());
        query.eq("area_id",entityMjYkm.getAreaId());
        EntityMjYkm entityMjYkm1 = daoMjYkm.selectOne(query);
        if (entityMjYkm1 == null){
            daoMjYkm.insert(entityMjYkm);
        }
        daoMjYkm.update(entityMjYkm,query);
    }
}
